package com.rati.usercenter.exception;

import com.rati.usercenter.common.BaseResponse;
import com.rati.usercenter.common.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.rati.usercenter.utils.ResultUtils.*;

/**
 * 全局异常处理器
 *
 * @author Lwh
 * @time 2022/12/14 19:55
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public BaseResponse<Object> businessException(BusinessException e) {
        log.error("BusinessException: " + e.getErrorCode().getMsg(), e);
        return fail(e.getErrorCode(), e.getDescription());
    }

    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<Object> runtimeException(RuntimeException e) {
        log.error("RuntimeException", e);
        return error(ErrorCode.SYSTEM_ERROR, "运行时异常");
    }

}
